package com.bibrApp.difu.api;

import java.net.URISyntaxException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;
import javax.net.ssl.X509TrustManager;

import io.socket.client.IO;
import io.socket.client.Socket;


/**
 * @author dj
 * @date 2020/3/21
 */
public class SocketIOClient {

    public Socket mSocket;

    public SocketIOClient() {
        initSocket();
    }

    private void initSocket() {
        try {
            IO.Options op = new IO.Options();
            op.transports = new String[]{"websocket"};
            mSocket = IO.socket("wss:dev-api.mixcdn.co/",op);
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 静态内部类，实例化对象使用
     */
    private static class SocketIOClientHelper {
        private static final SocketIOClient INSTANCE = new SocketIOClient();
    }

    public Socket getSocket() {
        return mSocket;
    }

    /**
     * 对外唯一实例的接口
     *
     * @return
     */
    public static SocketIOClient getInstance() {
        return SocketIOClientHelper.INSTANCE;
    }

    public static class BiBrTrustManager implements X509TrustManager {
        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }

        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];
        }
    }

    public static class BiBrHostnameVerifier implements HostnameVerifier {
        @Override
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    }
}